<?php

namespace App\Repositories\Eloquent\Mgt;


use Illuminate\Container\Container as App;
use App\Repositories\Eloquent\Repository;
use Illuminate\Support\Facades\Auth;
use DB;

class ActivityRepository extends Repository
{

    private $activity;
    /**
     * 指定模型名称
     *
     * @return mixed
     */
    function model()
    {
        return 'App\Models\Mgt\Activity';
    }


    public function index()
    {
        $staff = Auth::guard('admin')->user();

        if ($staff->branch->id !== 1) {
            $result = $this->model
                ->withCount('tracks','borrows','returns','joins','acts')
                ->where('branch_id', $staff->branch->id)
                ->orderBy('id', 'desc');
        }
        else {
            $result = $this->model
                ->withCount('tracks','borrows','returns','joins','acts')
                ->orderBy('id', 'desc');
        }

        return $result;
    }

    public function show($id)
    {
        $activity = $this->find($id);

        if (!$activity)
            return $this->responseFail(404, '活动信息不正确');

        return $activity;
    }


    public function store($request)
    {
        $date_begin = strtotime($request->date_begin);

        $date_end = strtotime($request->date_end);

        if ( $date_begin > $date_end)
            return $this->responseFail(500, '日期设置不正确');

        $activity = $this->create([
            'name'          => $request->name,
            'is_checkin'    => $request->checkin == 1?1:0,
            'is_show'       => $request->show == 1?1:0,
            'is_signin'     => $request->signin == 1?1:0,
            'branch_id'     => $request->branch_id,
            'date'          => time(),
            'status'        => STATUS_NORMAL,
            'date_begin'    => $date_begin,
            'date_end'      => $date_end,
            'desc'          => $request->desc,
        ]);

        //$route = $request->signin == 1?route('activity.special'):route('activity.normal');

        if (!!$activity) {
             return $this->responseSuccess('增加活动成功', $activity, route('activity.index'));
        }

        return $this->responseFail(500, '增加活动失败');
    }

    /**
     * 更新activity
     *
     * @param $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function renew($request, $id)
    {
        $date_begin = strtotime($request->date_begin);

        $date_end = strtotime($request->date_end);

        if ( $date_begin > $date_end)
            return $this->responseFail(500, '日期设置不正确');

        DB::beginTransaction();
        try {
            $result = $this->update([
                'name'          => $request->name,
                'is_checkin'    => $request->checkin == 1?1:0,
                'is_show'       => $request->show == 1?1:0,
                'is_signin'     => $request->signin == 1?1:0,
                'status'        => $request->status,
                'date_begin'    => $date_begin,
                'date_end'      => $date_end,
                'desc'          => $request->desc,
            ], $id);

            DB::commit();
            return $this->responseSuccess('更新活动信息成功');

        } catch (Exception $e) {
            DB::rollback();
            return $this->responseFail(500, '更新活动信息失败');
        }

    }


    public function status($request, $id)
    {
        $result = $this->update([
            'status' => $request->status,
            ], $id);

        if($result)
            return $this->responseSuccess('更新活动状态成功');

        return $this->responseFail(500, '更新活动状态失败');

    }

    /**
     * 活动统计
     * @return [type] [description]
     */
    public function activities()
    {
        $rows = $this->model->select('id')->withCount('borrows','joins','acts')->get();

        return collect($rows->toArray());
    }

}